<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

require_once './lib/function.inc.php';
require_once './lib/AopSdk.php';
require_once './lib/HttpRequst.php';
require_once './lib/UserInfo.php';
require_once './lib/PushMsg.php';
require_once './lib/config.php';
require_once './lib/MysqliDb.php';

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

    //系统首页
    public function index(){
        //客户端信息
        /*$user_agent = $_SERVER['HTTP_USER_AGENT'];

        $user=session('user');
        $auth_code=  \HttpRequest::getRequest ( "auth_code" );

        if(!stristr($user_agent,"AlipayClient") || !stristr($user_agent,"AlipayDefined")){
            header('Location:http://m.taobao.com'); // 不包含肯定不是支付宝访问的所以跳转网址吧
        }

        $config=C('ALI');
        if (!isset($user) && empty ( $auth_code )){ // 这里判断cokies不含有跳转到授权链接
            $ru= "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=".$config['app_id']."&scope=auth_userinfo&redirect_uri=".urlencode($config['url']);
            session("user",null);
            header('Location:'.$ru);
        }

        if(!empty($user)){
            $results =M('agent')->where (array('ali_user_id'=>$user['ali_user_id']))->find();
            session('user',$results);
        }

        if(! empty ( $auth_code ) && empty($results)){
            //获取用户资料并存库
            $userinfo = new \UserInfo ();
            $user_info_resp=$userinfo->getUserInfo ( $auth_code );

            $data=Array(
                "user_id" => $user_info_resp->user_id,
                "avatar" => $user_info_resp->avatar,
                "nick_name" => !empty($user_info_resp->nick_name)?$user_info_resp->nick_name:'',
                "is_certified" => !empty($user_info_resp->is_certified)?$user_info_resp->is_certified:'',
                "user_status" => !empty($user_info_resp->user_status)?$user_info_resp->user_status:'',
                "create_time"=>time()
            );

            if(!empty($user_info_resp->user_id)){
                $ali_user_id=$user_info_resp->user_id;
                $re =M('agent')->where (array('ali_user_id'=>$ali_user_id))->find();
                if(empty($re)){
                    M('agent')->add($data);
                }

            }

        }*/

        header ( "Content-type: text/html; charset=utf-8" );
        $config=M('config')->where(array('name'=>'RED_STAR_TIME'))->find();
        $red=M('red_envelopes')->where(array('type'=>1,'num'=>array('gt',0)))->find();


        $this->assign('config',$config);
        $this->assign('red',$red);
        $this->display();
    }



}